<template>
	<xn-form-container
		:title="formData.id ? '编辑' : '添加'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<!-- <a-form-item label="日程类型：" name="calendarType">
				<a-select v-model:value="formData.type" placeholder="请选择">
					<a-select-option :value="1">会议</a-select-option>
					<a-select-option :value="2">日程</a-select-option>
				</a-select>
			</a-form-item> -->
			<a-form-item label="日程类型：" name="calendarType">
				<a-radio-group v-model:value="formData.calendarType" name="radioGroup" :disabled="disabled">
					<a-radio :value="0">私密</a-radio>
					<a-radio :value="1">公开</a-radio>
				</a-radio-group>
			</a-form-item>
			<a-form-item name="date" label="日期">
				<a-date-picker
					v-model:value="formData.date"
					value-format="YYYY-MM-DD"
					:disabled="disabled"
					placeholder="请选择日期"
					style="width: 100%"
				/>
			</a-form-item>
			<a-form-item name="startDate" label="开始时间">
				<a-time-picker
					v-model:value="formData.startDate"
					value-format="HH:mm"
					:minute-step="30"
					format="HH:mm"
					:disabled="disabled"
					show-time
					placeholder="请选择开始时间"
					style="width: 100%"
				/>
			</a-form-item>
			<a-form-item name="endDate" label="结束时间">
				<a-time-picker
					v-model:value="formData.endDate"
					format="HH:mm"
					:minute-step="30"
					value-format="HH:mm"
					:disabled="disabled"
					show-time
					placeholder="请选择结束时间"
					style="width: 100%"
				/>
			</a-form-item>
			<a-form-item label="摘要：" name="abstractstr">
				<a-input :disabled="disabled" v-model:value="formData.abstractstr" placeholder="请输入摘要" allow-clear />
			</a-form-item>
			<a-form-item label="地点：" name="locations">
				<a-input :disabled="disabled" v-model:value="formData.locations" placeholder="请输入地点" allow-clear />
			</a-form-item>
			<a-form-item label="具体事宜：" name="content">
				<a-input :disabled="disabled" v-model:value="formData.content" placeholder="请输入具体事宜" allow-clear />
			</a-form-item>
			<!-- <a-form-item label="所属用户：" name="userId">
				<a-input :disabled="disabled" v-model:value="formData.userId" placeholder="请输入所属用户" allow-clear />
			</a-form-item> -->
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" v-if="!disabled" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="bizCalendarManagementForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import bizCalendarManagementApi from '@/api/biz/bizCalendarManagementApi'
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	const disabled = ref(false)
	// 表单数据
	const formData = ref({
		calendarType: '',
		date: null,
		startDate: undefined,
		endDate: undefined,
		abstractstr: '',
		locations: '',
		content: ''
	})
	const submitLoading = ref(false)
	// 编辑和查看详情设置数据
	const setData = (val) => {
		console.log(val, 'setData-----')
		formData.value = { ...val }
	}
	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		disabled.value = false
		if (record) {
			console.log(record, '--record')
			disabled.value = true
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
		calendarType: [required('请选择日程类型')]
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			console.log(88888, formData.value.date)
			submitLoading.value = true
			const objID = formData.value.id ? { id: formData.value.id } : {}
			const formDataParam = cloneDeep({
				calendarType: formData.value.calendarType,
				startDate: `${formData.value.date} ${formData.value.startDate}`,
				endDate: `${formData.value.date} ${formData.value.endDate}`,
				abstractstr: formData.value.abstractstr,
				locations: formData.value.locations,
				content: formData.value.content
			})
			bizCalendarManagementApi
				.bizCalendarManagementSubmitForm(Object.assign(formDataParam, objID), formData.value.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	// 抛出函数
	defineExpose({
		onOpen,
		setData
	})
</script>
